<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @submit.native.prevent
        v-show="showSearch"
        label-width="85px"
      >
        <el-form-item label="业务系统" prop="applicationSystem">
          <el-select
            v-model="queryParams.applicationSystem"
            class="custom-select"
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option
              class="custom-select"
              v-for="(item, index) in businessCodeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictLabel"
            >
              <span class="selectList">
                <common-marquee-text :content="item.dictLabel">
                </common-marquee-text>
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库名/用户名" prop="dbAccount">
          <common-input
            v-model="queryParams.dbAccount"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="数据库名称" prop="dbName">
          <common-input
            v-model="queryParams.dbName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="数据库类型" prop="dbType">
          <el-select
            v-model="queryParams.dbType"
            style="width: 100%"
            filterable
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in dbTypeList"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="表名" prop="tableName">
          <common-input
            v-model="queryParams.tableName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="列名" prop="columnName">
          <common-input
            v-model="queryParams.columnName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="识别规则" prop="ruleIds">
          <el-select v-model="queryParams.ruleIds" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in ruleList"
              :key="item.id"
              :label="item.ruleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="识别方式" prop="discernType">
          <el-select
            v-model="queryParams.discernType"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option label="自动识别" :value="0"></el-option>
            <el-option label="人工识别" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>

    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-switch
          v-model="isMatchNums"
          @change="matchNumChange"
          active-color="#13ce66"
          inactive-color="#909399"
          active-text="仅含有敏感字段数据"
          inactive-text="所有数据"
        >
        </el-switch>
      </el-col>
    </el-row>
    <el-row slot="tools" :gutter="10" class="mb8" style="margin-top: 15px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          :loading="exportLoading"
          @click="handleExport"
          >导出
        </el-button>
      </el-col>
    </el-row>
    <common-table slot="content" v-loading="loading" :data="list">
      <el-table-column
        label="业务系统"
        align="left"
        prop="applicationSystem"
        fixed
      />
      <el-table-column label="数据库名称" align="left" prop="dbName" />
      <el-table-column
        label="数据库类型"
        align="left"
        prop="dbType"
        show-overflow-tooltip
      />
      <el-table-column
        label="库名/用户名"
        align="left"
        prop="dbAccount"
        show-overflow-tooltip
      />
      <el-table-column
        label="表名"
        align="left"
        prop="tableName"
        show-overflow-tooltip
      />
      <el-table-column
        label="列名"
        align="left"
        prop="columnName"
        show-overflow-tooltip
      />
      <el-table-column
        label="列备注"
        align="left"
        prop="columnRemark"
        show-overflow-tooltip
      />
      <el-table-column label="识别方式" align="left" prop="discernType">
        <template slot-scope="scope">
          {{ scope.row.discernType === 0 ? "自动识别" : "人工识别" }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="匹配结果" align="left" prop="approve">
        <template slot-scope="scope">
          <el-tag
            type="success"
            effect="plain"
            v-if="scope.row.approve === '准确'"
            >{{ scope.row.approve }}</el-tag
          >
          <el-tag
            type="danger"
            effect="plain"
            v-else-if="scope.row.approve === '不准确'"
            >{{ scope.row.approve }}</el-tag
          >
          <span v-else>{{ scope.row.approve }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="采样数据"
        align="left"
        prop="remark"
        show-overflow-tooltip
      />
      <el-table-column
        label="IP"
        align="left"
        prop="ip"
        show-overflow-tooltip
      />
      <el-table-column
        label="表行数"
        align="left"
        prop="scanNums"
        show-overflow-tooltip
      />
      <el-table-column
        label="数据分类"
        align="left"
        prop="typeId"
        show-overflow-tooltip
      />
      <el-table-column
        label="数据分级"
        align="left"
        prop="levelId"
        show-overflow-tooltip
      />
      <el-table-column
        label="识别规则"
        align="left"
        prop="label"
        show-overflow-tooltip
      />
      <el-table-column
        label="重要数据"
        align="left"
        prop="importantData"
        show-overflow-tooltip
      />
      <el-table-column
        label="核心数据"
        align="left"
        prop="coreData"
        fixed="right"
        show-overflow-tooltip
      />
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </common-list>
</template>

<script>
import {
  queryListByTypeCode,
  queryTableColumnList,
  querySensitiveWordsList,
  querySensitiveAllWordsList,
} from "@/api/datascan/scanresults/column";
import Utils, { formatDate } from "@/utils";
import BaseList from "@/mixins/BaseList";
import { decode } from "@/utils/aesUtils";
import { mapGetters } from "vuex";
import { queryList } from "@/api/datahandler/discernRule";
import { queryBusinessList } from "@/api/datascan/dbinfo/info";
export default {
  name: "Info",
  mixins: [BaseList],
  components: {},
  data() {
    return {
      exportLoading: false,
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 列的基本信息表格数据
      list: [],
      wordsList: [],
      allWordsList: [],
      dbTypeList: [
        {
          label: "oracle",
          value: "oracle",
        },
        {
          label: "mysql",
          value: "mysql",
        },
        {
          label: "teledb",
          value: "teledb",
        },
        {
          label: "pgsql",
          value: "pgsql",
        },
      ], // 数据库类型
      isMatchNums: true,
      // 查询参数
      queryParams: {
        isPass: 1,
        pageNum: 1,
        pageSize: 10,
        ruleIds: null,
        matchNums: 0, // 是否包含敏感字符  所有数据时为空字符串 仅包含敏感词时为 0
        dbType: null,
        dbName: null,
        tableName: null,
        wordId: null,
        discernType: null,
        columnName: null,
        telephone: null,
        deptIds: null,
        dbAccount: null,
        applicationSystem: null,
      },
      infoShow: false,
      info: {
        dbType: null,
        columnType: null,
        dbName: null,
        dbAccount: null,
        tableName: null,
        columnName: null,
        columnRemark: null,
        label: null,
        discernType: null,
        isApprove: "--",
        remark: null,
        createTime: null,
        ip: null,
        scanNums: null,
        typeId: null,
        levelId: null,
        principal: null,
        telephone: null,
      },
      approveList: [
        {
          label: "--",
          value: null,
        },
        {
          label: "--", // 重置
          value: 0,
        },
        {
          label: "准确",
          value: 1,
        },
        {
          label: "不准确",
          value: 2,
        },
        {
          label: "准确",
          value: 3,
        },
      ],

      fieldShow: false,
      fieldLabel: "",
      fieldForm: {
        wordIds: null,
        scanResultId: null,
        isApprove: 3,
      },
      currentParent: 4,
      singleDeptList: [],

      itemInfo: null,
      // --------重构设置重要数据----------
      showImportData: false,
      showCoreData: false,
      showDiserRule: false,
      ruleList: [],
      businessCodeList: [],
    };
  },
  computed: {
    ...mapGetters(["systemInfo"]),
  },
  created() {
    this.getList();
    // this.getWordsList();
    this.getDataSourceTypeList();
    this.getQueryWordList();
    this.getRuleList();
    this.queryBusinessCodeList();
  },
  methods: {
    //获取业务系统列表
    queryBusinessCodeList() {
      queryBusinessList().then((res) => {
        if (res && res.code === 200) {
          this.businessCodeList = res.data;
        } else {
          this.businessCodeList = [];
        }
      });
    },
    // 获取识别规则
    getRuleList() {
      queryList({ status: 0 }).then((res) => {
        if (res && res.code === 200) {
          this.ruleList = res.rows;
        } else {
          this.dataList = [];
        }
      });
    },

    handleExport() {
      this.exportLoading = true;
      this.download(
        "datascan/b/dbscanresult/exportTemplate",
        {
          ...this.queryParams,
          pageSize: 10000,
        },
        `扫描结果_列_${new Date().getTime()}.xlsx`,
        1800000
      ).finally(() => {
        this.exportLoading = false;
      });
    },

    /** 查询列的基本信息列表 */
    getList() {
      this.loading = true;
      queryTableColumnList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.data.dataList.reduce((r, c) => {
              let approve = this.approveList.find(
                (a) => a.value === c.isApprove
              );
              c.approve = approve ? approve.label : "--";
              if (c.telephone !== "" && c.telephone !== null) {
                c.telephone = Utils.phoneEncryption(decode(c.telephone));
              } else {
                c.telephone = "";
              }
              r.push(c);
              return r;
            }, []);
            this.total = res.data.totalCount;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      // this.$refs.commonDept.clearDept();
    },

    getWordsList() {
      querySensitiveWordsList({}).then((res) => {
        if (res && res.code === 200) {
          this.wordsList = res.data.dataList.reduce((r, c) => {
            r.push({
              label: c.name,
              value: c.wordId,
            });
            return r;
          }, []);
        } else {
          this.wordsList = [];
        }
      });
    },
    getQueryWordList() {
      querySensitiveAllWordsList().then((res) => {
        if (res && res.code === 200) {
          this.allWordsList = res.data.dataList.reduce((r, c) => {
            r.push({
              label: c.name,
              value: c.wordId,
            });
            return r;
          }, []);
        } else {
        }
      });
    },

    matchNumChange() {
      this.queryParams.matchNums = this.isMatchNums ? 0 : "";
      this.handleQuery();
    },

    getDataSourceTypeList() {
      queryListByTypeCode().then((res) => {
        if (res && res.code === 200) {
          this.dbTypeList = res.data.dataList.reduce((r, c) => {
            r.push({
              label: c.name,
              value: c.value,
            });
            return r;
          }, []);
        } else {
          this.dbTypeList = [];
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.el-dropdown-menu__item {
  font-size: 12px;
}
::v-deep .info-table-row-value {
  overflow-y: auto;
}

.el-dropdown-link {
  color: var(--theme-color);
}
</style>
